var WxNotificationCenter = require('../../../utils/WxNotificationCenter');

Page({
    data: {
        address: {}, // 当前编辑的地址
        isEdit: false, // 是否是编辑模式
        area: '' // 存储选择的区域信息
    },

    onLoad: function (options) {
        // 注册通知
        WxNotificationCenter.addNotification("poiSelectedNotification", this.getAddress, this);
        
        if (options.objectId) {
            this.loadAddress(options.objectId);
            this.setData({
                isEdit: true
            });
            wx.setNavigationBarTitle({
                title: '编辑地址'
            });
        } else {
            wx.setNavigationBarTitle({
                title: '添加地址'
            });
        }
    },

    selectAddress: function () {
        wx.navigateTo({
            url: '../search/search'
        });
    },

    getAddress: function (area) {
        this.setData({
            area: area
        });
    },

    add: function (e) {
        const form = e.detail.value;

        if (!form.realname) {
            wx.showModal({
                title: '请填写收件人姓名',
                showCancel: false
            });
            return;
        }

        if (!(/^1[34578]\d{9}$/.test(form.mobile))) {
            wx.showModal({
                title: '请填写正确手机号码',
                showCancel: false
            });
            return;
        }

        if (!form.detail) {
            wx.showModal({
                title: '请填写详细地址',
                showCancel: false
            });
            return;
        }

        form.gender = parseInt(form.gender);

        wx.getStorage({
            key: 'addresses',
            success: (res) => {
                let addresses = res.data || [];
                if (this.data.isEdit) {
                    addresses = addresses.map(address => {
                        if (address.id === this.data.address.id) {
                            return { ...form, id: address.id };
                        }
                        return address;
                    });
                } else {
                    form.id = new Date().getTime();
                    addresses.push(form);
                }

                wx.setStorage({
                    key: 'addresses',
                    data: addresses,
                    success: () => {
                        wx.showModal({
                            title: '保存成功',
                            showCancel: false,
                            success: () => {
                                wx.navigateBack();
                            }
                        });
                    }
                });
            },
            fail: () => {
                form.id = new Date().getTime();
                const addresses = [form];
                wx.setStorage({
                    key: 'addresses',
                    data: addresses,
                    success: () => {
                        wx.showModal({
                            title: '保存成功',
                            showCancel: false,
                            success: () => {
                                wx.navigateBack();
                            }
                        });
                    }
                });
            }
        });
    },

    loadAddress: function (objectId) {
        wx.getStorage({
            key: 'addresses',
            success: (res) => {
                const addresses = res.data || [];
                const address = addresses.find(addr => addr.id === parseInt(objectId));
                if (address) {
                    this.setData({
                        address: address
                    });
                }
            }
        });
    },

    deleteAddress: function () {
        wx.showModal({
            title: '确认删除',
            success: (res) => {
                if (res.confirm) {
                    wx.getStorage({
                        key: 'addresses',
                        success: (res) => {
                            let addresses = res.data || [];
                            addresses = addresses.filter(address => address.id !== this.data.address.id);

                            wx.setStorage({
                                key: 'addresses',
                                data: addresses,
                                success: () => {
                                    wx.showModal({
                                        title: '删除成功',
                                        showCancel: false,
                                        success: () => {
                                            wx.navigateBack();
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            }
        });
    }
});
